<?php

/*
 +-----------------------------------------------------------------------+
 | plugins/sieverules/sieverules_ajax.inc                                |
 |                                                                       |
 | This file is part of the RoundCube Webmail client                     |
 | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland                 |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 | PURPOSE:                                                              |
 |	 Provide ajax functionality for SieveRules plugin                    |
 |                                                                       |
 +-----------------------------------------------------------------------+
 | Author: Aleksander Machniak <alec@alec.pl>                            |
 | Modifications by: Philip Weir                                         |
 +-----------------------------------------------------------------------+
 | Requires: plugins/sauserprefs/sieverules_func.inc                     |
 +-----------------------------------------------------------------------+

 $Id: $

*/

require_once('plugins/sieverules/sieverules_func.inc');

if ($operation = get_input_value('_operation', RCUBE_INPUT_GPC)) {
	sieverules_init();

	$iid = get_input_value('_iid', RCUBE_INPUT_GET);

	if ($operation == 'up' && !$SIEVE_ERROR) {
		if ($iid && isset($SCRIPT[$iid]) && isset($SCRIPT[$iid-1])) {
			if ($SIEVE->script->update_rule($iid, $SCRIPT[$iid-1]) !== false && $SIEVE->script->update_rule($iid-1, $SCRIPT[$iid]) !== false)
				$result = $SIEVE->save();

			if ($result)
				$OUTPUT->command('sieverules_updatelist', 'up', '', $iid);
			else
				$OUTPUT->show_message('filtersaveerror', 'error');
		}
	}
	elseif ($operation == 'down' && !$SIEVE_ERROR)	{
		if (isset($SCRIPT[$iid]) && isset($SCRIPT[$iid+1]))	{
			if ($SIEVE->script->update_rule($iid, $SCRIPT[$iid+1]) !== false && $SIEVE->script->update_rule($iid+1, $SCRIPT[$iid]) !== false)
				$result = $SIEVE->save();

			if ($result)
				$OUTPUT->command('sieverules_updatelist', 'down', '', $iid);
			else
				$OUTPUT->show_message('filtersaveerror', 'error');
		}
	}

	$OUTPUT->send();
}

?>